home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1409 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  88 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Splitting String ?
  5. Date: 13 Jan 1996 19:12:18 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Distribution: world
  8. Message-ID: <4d906i$pq2@news.iag.net>
  9. References: <HAKOLA.96Jan12151128@jung.hut.fi> <4d67an$303@unda.fi>
  10. NNTP-Posting-Host: pm2-orl3.iag.net
  11. X-Newsreader: WinVN 0.99.7
  12.  
  13. In article <4d67an$303@unda.fi>, olle@gustav.unda.fi says...
  14. ~
  15. ~In article <HAKOLA.96Jan12151128@jung.hut.fi>, hakola@cadmail.hut.fi (Petri 
  16. Hakola) writes:
  17. ~ ~
  18. ~ ~      Have I missed something (again:)
  19. ~
  20. ~Yes...
  21. ~
  22. ~ ~                                       or why doesn't this code
  23. ~ ~      work? I should split dos-a-like-filename and add new postfix
  24. ~ ~      instead of old one (i.e. DATA.TXT --> DATA.UPD  It seems to
  25. ~ ~      work correctly if the filename has an old postfix, but if
  26. ~ ~      there isn't one start won't return what it should.
  27. ~ ~
  28. <snip>
  29. ~ ~ main() {
  30. ~ ~   printf("%s\n",newname("LONGNAME.TXT"));
  31. ~ ~   printf("%s\n",newname("NOEND"));
  32. ~ ~ }
  33. ~
  34. ~(In the prevous code the variable start is not really needed, but wait...)
  35. ~
  36. ~You'll need to e.g. malloc() in newname for a string with enough room for the
  37. ~original string (w/o the extension) plus end (plus '\0') and copy the input
  38. ~string / head of the input string there and _then_ strcat on that!
  39. ~
  40. ~(You cannot simply strcat on "NOEND" and I don't think that it is a great 
  41. idea
  42. ~to modify "LONGNAME.TXT", either, even if it just happens to work in your
  43. ~environment, and even if this is just an example of using newname!)
  44.  
  45. Right, he can't safely modify either one.  String literals can legally be
  46. stored as read only.  So, the ability to modify them is implementation
  47. defined.  It is always safest to treat them as const.
  48.  
  49. His attempt to add an extension to "NOEND" is undoubtedly what caused the
  50. failure, since it would have overwritten whatever happened to follow it.
  51.  
  52.  
  53. A safe version might go something like:
  54.  
  55. terms:
  56.        FileNameLen  = max length for filename with no extension or dot.
  57.        ExtLen       = max length of extension with a dot.
  58.  
  59. 1. Define function to accept a const char * and return a char * (or const).
  60.  
  61. 2. Define: char *dot; char *ext = ".txt" /* your extension string */
  62.  
  63. 3. Define a char array (ary) of FileNameLen + ExtLen + 1 (for '\0'). Either
  64.     A. malloc it (the caller will need to free memory later).
  65.     B. define a (local or global) static (the contents will be overwritten
  66.        on each call).
  67.  
  68. 4. Use strrchr to set dot to the last '.' on the source string.
  69.  
  70. 5. Protect the bounds of ary:
  71.      If dot == NULL, test strlen of source against FileNameLen
  72.      else test (dot - source) against FileNameLen
  73.        A. Do not procede without handling any error.
  74.   
  75. 6. copy the source to your ary.
  76.  
  77. 7. if dot == NULL, use strcat to append your extension to ary
  78.    else use strcpy to append it to ary at ary + (dot - source)
  79.  
  80. 8. return ary
  81.  
  82. Did I miss anything?
  83.  
  84. -- 
  85. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  86. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  87.  
  88.